package com.zmn.oms.api.controller.third.koubei;

import com.alibaba.fastjson.JSON;
import com.taobao.api.internal.spi.CheckResult;
import com.taobao.api.internal.spi.SpiUtils;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.third.koubei.KoubeiConsts;
import com.zmn.oms.third.koubei.dto.KoubeiCreateOrderResponse;
import com.zmn.oms.third.koubei.dto.KoubeiFailedResponse;
import com.zmn.oms.third.koubei.dto.KoubeiQueryInventoryResponse;
import com.zmn.oms.third.koubei.service.KoubeiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 类描述：口碑系列渠道对接接口，用于接受渠道创建和库存查询订单
 *
 * @author xiewenbing
 * @date 2021/08/16 18:00
 */

@RestController
@RequestMapping("/koubei")
@Slf4j
public class KoubeiController {

    @Autowired
    private KoubeiService koubeiService;

    @PostMapping("/api/createOrder")
    @ResponseBody
    public KoubeiFailedResponse createOrder(HttpServletRequest httpServletRequest) {

        // 签名验证
        CheckResult checkResult = null;
        try {
            checkResult = SpiUtils.checkSign(httpServletRequest, KoubeiConsts.APP_SECRET);
        } catch (Exception e) {
            log.info("【{}】签名验证异常", KoubeiConsts.CHANNEL_NAME);
        }

        if (Objects.isNull(checkResult) || !checkResult.isSuccess()) {
            return KoubeiFailedResponse.verifySignFailed();
        }

        String requestBody = checkResult.getRequestBody();
        if (StringUtil.isBlank(requestBody)) {
            return KoubeiFailedResponse.handleFailed("请求内容为空");
        }

        log.info("【{}】收到创建订单请求【{}】",KoubeiConsts.CHANNEL_NAME,checkResult.getRequestBody());

        try {
            KoubeiCreateOrderResponse response = koubeiService.handleCreateOrderRequest(checkResult.getRequestBody());
            log.info("【{}】处理创建订单请求应答数据【{}】",KoubeiConsts.CHANNEL_NAME, JSON.toJSONString(response));

            return response;
        } catch (OmsBaseException oe) {
            log.info("【{}】处理请求失败", KoubeiConsts.CHANNEL_NAME, oe.getMessage());
            return KoubeiFailedResponse.handleFailed(oe.getMessage());
        }
    }

    @PostMapping("/api/queryInventory")
    @ResponseBody
    public KoubeiFailedResponse queryInventory(HttpServletRequest httpServletRequest) {

        // 签名验证
        CheckResult checkResult = null;
        try {
            checkResult = SpiUtils.checkSign(httpServletRequest, KoubeiConsts.APP_SECRET);
        } catch (Exception e) {
            log.info("【{}】签名验证异常", KoubeiConsts.CHANNEL_NAME);
        }

        if (Objects.isNull(checkResult) || !checkResult.isSuccess()) {
            return KoubeiFailedResponse.verifySignFailed();
        }

        String requestBody = checkResult.getRequestBody();
        if (StringUtil.isBlank(requestBody)) {
            return KoubeiFailedResponse.handleFailed("请求内容为空");
        }

        log.info("【{}】收到库存查询请求【{}】",KoubeiConsts.CHANNEL_NAME, checkResult.getRequestBody());

        try {
            KoubeiQueryInventoryResponse response = koubeiService.handleInventoryQueryRequest(checkResult.getRequestBody());
            log.info("【{}】处理库存查询请求应答数据【{}】",KoubeiConsts.CHANNEL_NAME, JSON.toJSONString(response));

            return response;
        } catch (OmsBaseException oe) {
            log.info("【{}】处理请求失败", KoubeiConsts.CHANNEL_NAME, oe.getMessage());
            return KoubeiFailedResponse.handleFailed(oe.getMessage());
        }
    }
}
